/*
 * MySqlRpc.cpp
 *
 *  Created on: Sep 23, 2015
 *      Author: rpi
 */

#include <iostream>
#include <map>
#include <jsonrpc.h>
#include <json/json.h>

#include "MySqlRpc.h"

MySqlRpc::MySqlRpc()
{
	poMysqlInterface = new dba::MysqlInterface();

	_mapMethods.clear();
	_mapMethods["getDeviceList"] = &MySqlRpc::getDeviceList;
	_mapMethods["getInstanceList"] = &MySqlRpc::getInstanceList;
}

MySqlRpc::~MySqlRpc()
{
	if (poMysqlInterface != NULL)
		delete poMysqlInterface;
}

bool MySqlRpc::getDeviceList(const Json::Value& root, Json::Value& response)
{
	std::cout << "Receive query: " << root << std::endl;
	response["jsonrpc"] = root["jsonrpc"];
	response["id"] = root["id"];
	//response["result"] = "success";

	try
	{
		response["result"] = poMysqlInterface->getDeviceList();
	}
	catch (std::string &ex)
	{
		response["error"] = ex;
	}

	return true;
}

bool MySqlRpc::getInstanceList(const Json::Value& root, Json::Value& response)
{
	std::cout << "Receive query: " << root << std::endl;
	response["jsonrpc"] = root["jsonrpc"];
	response["id"] = root["id"];
	//response["result"] = "success";

	try
	{
		response["result"] = poMysqlInterface->getInstanceList();
	}
	catch (std::string &ex)
	{
		response["error"] = ex;
	}

	return true;
}

Json::Rpc::RpcMethod<MySqlRpc>::Method MySqlRpc::getRpcMethod(std::string func)
{
	return _mapMethods[func];
}
